using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using OpenIddict.Abstractions;
using OpenIddict.Server;
using static OpenIddict.Server.OpenIddictServerEvents;

namespace Identity.HttpAPI.Host.Extensions
{
   public class DebugApplyTokenResponseHandler : IOpenIddictServerHandler<OpenIddictServerEvents.ProcessSignInContext>
{
    public ValueTask HandleAsync(OpenIddictServerEvents.ProcessSignInContext context)
    {
        var principal = context.Principal;

        if (principal is not null)
        {
            Console.WriteLine("=== ApplyTokenResponse Principal Claims ===");
            foreach (var claim in principal.Claims)
            {
                var destinations = claim.GetDestinations().ToArray() ?? Array.Empty<string>();
                Console.WriteLine($"Type: {claim.Type}, Value: {claim.Value}, Destinations: {string.Join(",", destinations)}");
            }
            Console.WriteLine("===========================================");
        }
        else
        {
            Console.WriteLine("ApplyTokenResponse: context.Principal is null ❌");
        }

        return default;
    }
}
}